package com.yc.chaoshi.cs_manage.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.chaoshi.cs_manage.service.IUmsRolesService;
import com.yc.chaoshi.entity.UmsRoles;
import com.yc.common.api.CommonResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xz
 * @since 2022-08-04
 */
@RestController
@RequestMapping("/umsRoles")
public class UmsRolesController {

    @Resource
    IUmsRolesService rolesService;
    @GetMapping("list")
   public CommonResult list(){
        List<UmsRoles> list=rolesService.list();
       return CommonResult.success(list);
   }

    /**
     * 根据名字查询所有角色
     * @param pageNum
     * @param pageSize
     * @param sreachname
     * @return
     */
    @PostMapping("listAll")
    public CommonResult listAll(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
                                @RequestParam(value = "sreachname") String sreachname){
        Page page = rolesService.listAll(pageNum,pageSize,sreachname);
        return CommonResult.success(page);
    }

    /**
     * 修改权限管理员是否启用状态
     */
    @GetMapping("/updatestatus/{id}/{status}")
    public CommonResult updatestatus(@PathVariable Integer id,@PathVariable Integer status){
        boolean result=rolesService.updatestatus(id,status);
        if (result){
            return CommonResult.success(result);
        }else {
            return CommonResult.failed();
        }
    }
    /**
     * 删除管理员
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    public CommonResult delete(@PathVariable Integer id){
        System.out.println(id);
        boolean result=this.rolesService.removeById(id);
        if (result){
            return CommonResult.success(result);
        }else {
            return CommonResult.failed();
        }
    }
    /**
     * 编辑管理员信息
     * @param umsRoles
     * @return
     */
    @PostMapping("edit")
    @ResponseBody
    public CommonResult edit(@RequestBody @Validated UmsRoles umsRoles){
        Boolean result=rolesService.edit(umsRoles);
        if (result){
            return CommonResult.success(result);
        }else {
            return CommonResult.failed();
        }
    }
    /**
     * 添加管理员信息
     * @param umsRoles
     * @return
     */
    @PostMapping("add")
    @ResponseBody
    public CommonResult add(@RequestBody @Validated UmsRoles umsRoles){
        Boolean result=rolesService.add(umsRoles);
        if (result){
            return CommonResult.success(result);
        }else {
            return CommonResult.failed();
        }
    }

    @PostMapping("addResource/{roleid}")
    public CommonResult addResource(@RequestBody List<Integer> ids,@PathVariable Integer roleid){
        Boolean result=rolesService.addResource(ids,roleid);
        if (result){
            return CommonResult.success(result);
        }else {
            return CommonResult.failed();
        }
    }
}

